<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
// Include the JLog class.
jimport('joomla.log.log');

// Initialise a basic logger with no options (once only).
JLog::addLogger(array());

// Add a message.

/**
 * KMS_Video View
 */
class KMS_VideoViewKMS_Video extends JView
{
		
        /**
         * display method of KMS_Video view
         * @return void
         */
        public function display($tpl = null) 
        {
                // get the Data
                $form = $this->get('Form');
                $item = $this->get('Item');
                
 
                // Check for errors.
                if (count($errors = $this->get('Errors'))) 
                {
                        JError::raiseError(500, implode('<br />', $errors));
                        return false;
                }
                // Assign the Data
                $this->form = $form;
                $this->item = $item;
			
                // Set the toolbar
                $this->addToolBar();
				$this->segments = $this->getSegments();
				
                // Display the template
                parent::display($tpl);
        }
 
        /**
         * Setting the toolbar
         */
         
        public function getSegments(){
			
				$db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query='select segment_id, time_frame from #__kms_segments where video_id = ' . $this->item->video_id;
                $db->setQuery((string)$query);
                $segments = $db->loadObjectList();
                //JLog::add('SEGMENTS = ' . gettype($segments))
                return $segments;
		} 
        protected function addToolBar() 
        {
			$layout = $this->getLayout();
			
                $input = JFactory::getApplication()->input;
                $input->set('hidemainmenu', true);
                $isNew = ($this->item->video_id == 0);
                $issegment = $input->getString('segment_id','');
				
                if ($layout == 'edit'){
					JToolBarHelper::title($isNew ? JText::_('COM_KMS_VIDEO_MANAGER_KMS_VIDEO_NEW')
												 : JText::_('COM_KMS_VIDEO_MANAGER_KMS_VIDEO_EDIT'));
					JToolBarHelper::save('kms_video.save');
					JToolBarHelper::cancel('kms_video.cancel', $isNew ? 'JTOOLBAR_CANCEL'
																	   : 'JTOOLBAR_CLOSE');
				}
				else{
					
						JToolBarHelper::title(JText::_($this->item->video_name));
						JToolBarHelper::deleteList('', 'kms_video_segments.delete');
						JToolBarHelper::editList('kms_video_segment.edit');
						JToolBarHelper::addNew('kms_video_segment.add');
						JToolBarHelper::cancel();
						
					}
        }
}
